package Step3;


    import java.util.Scanner;

    public class Library {
        public static final int SIZE = 100; //图书库最大藏书量
        Book[] booklist; //图书库
        int count;		//当前图书库的图书数量
        Scanner scan;

        public Library() {
            booklist = new Book[SIZE];
            //初始化图书库，默认里面有5本书，信息如下
            booklist[0] = new Book("Java编程思想", "埃克尔", "机械工业出版社", "9787111213826", 78.0);
            booklist[1] = new Book("C++程序设计", "陈可", "电子工业出版社",  "9787145513853", 45.6);
            booklist[2] = new Book("程序设计基础", "张欣易", "北京大学出版社", "9784380850350",35.0);
            booklist[3] = new Book("Linux入门 ", "李旺", "机械工业出版社", "9784254465775",56.7);
            booklist[4] = new Book("Unix编程大全", "程查斯", "清华大学出版社", "9784343465729",128.5);

            count = 5;
            scan = new Scanner(System.in);
        }

        public void run(){

            int userChoice = 0;
            //循环处理，直到用户选择了“0...退出”
            do{
                displayMenu();

                //读取用户输入
                userChoice = scan.nextInt();

                switch(userChoice){
                    case 0:
                        System.out.println("成功退出系统，欢迎再次使用！");
                        break;
                    case 1:
                        printAllBook();
                        break;
                    case 2:
                        addBook();
                        break;
                    case 3:
                        deleteBook();
                        break;
                    case 4:
                        modifyBook();
                        break;
                    case 5:
                        findBook();
                        break;
                    default:
                        System.out.println("输入非法,请重新输入！");

                }
            }while(userChoice != 0);
            scan.close();
        }

        void displayMenu(){

            //打印菜单

            System.out.println("---------------------");
            System.out.println("    图书管理系统     ");
            System.out.println("---------------------");
            System.out.println("|   0...退出系统	|");
            System.out.println("|   1...显示图书	|");
            System.out.println("|   2...增加图书	|");
            System.out.println("|   3...删除图书	|");
            System.out.println("|   4...修改图书	|");
            System.out.println("|   5...查询图书	|");
            System.out.println("---------------------");
            System.out.print("请输入选项：");
        }

        void addBook(){  //增加图书
            if (count < SIZE){
                System.out.println("----------------------------------------------");

                System.out.print("请输入图书名：");
                String title = scan.next();
                System.out.print("请输入作者：");
                String author = scan.next();
                System.out.print("请输入出版社：");
                String press = scan.next();
                System.out.print("请输入ISBN：");
                String ISBN = scan.next();
                System.out.print("请输入单价：");
                double price = scan.nextDouble();
                Book book = new Book(title,author,press,ISBN,price);
                booklist[count] = book;
                count++;
                System.out.println("成功增加1本图书！当前图书库信息如下：");

                printAllBook();

            }
            else{
                System.out.println("图书库已满！");
            }
        }

        void deleteBook(){   //删除图书

            int id = -1;
            while(true){
                printAllBook();
                System.out.print("请输入序号删除图书，输入0返回主菜单: ");
                int userChoice = scan.nextInt();
                if(userChoice == 0){
                    break;
                }
                else {
                    id = findByOrder(userChoice);
                    if(id > -1)	{
                        for(int j = id; j < count-1; j++){  //用for循环的形式实现对数组的删除
                            booklist[j] = booklist[j+1];
                        }
                        count --;
                        System.out.println("删除成功！");
                    }
                    else
                        System.out.println("输入错误！请重新输入！");
                }
            }
        }

        void modifyBook() {   //修改图书
            while(true)	{
                printAllBook();

                System.out.print("请输入序号修改图书，输入0返回主菜单: ");
                int userChoice = scan.nextInt();
                int id = -1;
                if(userChoice == 0){
                    break;
                }
                else {
                    id = findByOrder(userChoice);
                    //***********Begin************
                    //调用modifyBookById修改第id本图书
                    //若修改成功，输出"修改成功！当前图书信息如下："并打印图书信息
                    //若输入有误，请提示："输入错误！请重新输入！"
                    if(modifyBookById(id)==true)
                    {
                        System.out.println("修改成功！当前图书信息如下：");
                        booklist[id].printInfo();
                    }
                    else
                        System.out.println("输入错误！请重新输入！");
                    //************End*************);

                }
            }
        }

        boolean modifyBookById(int id){  //修改第id本图书（id为数组下标，从0开始）

            if(id > -1 && id < count)	{
                System.out.println("图书信息如下：");
                booklist[id].printInfo();
                //此处获得图书的原各项信息
                String titleOld = booklist[id].getTitle();
                String authorOld = booklist[id].getAuthor();
                String pressOld = booklist[id].getPress();
                String ISBNOld = booklist[id].getISBN();
                double priceOld = booklist[id].getPrice();
                scan.nextLine();  //释放前一个回车

                System.out.print("请输入新书名(直接回车保持不变)：");
                String title = scan.nextLine();
                title = title.isEmpty() ? titleOld : title;
                //***********Begin************
                //仿造上面三行代码，提示用户输入其它信息
                //变量名请用author,press,ISBN
                System.out.print("请输入作者(直接回车保持不变)：");
                String author = scan.nextLine();
                author = author.isEmpty() ? authorOld : author;

                System.out.print("请输入出版社(直接回车保持不变)：");
                String press = scan.nextLine();
                press = press.isEmpty() ? pressOld : press;

                System.out.print("请输入ISBN(直接回车保持不变)：");
                String ISBN = scan.nextLine();
                ISBN = ISBN.isEmpty() ? ISBNOld : ISBN;
                //************End*************);

                System.out.print("请输入单价(直接回车保持不变)：");
                String strPrice = scan.nextLine();
                double price = strPrice.isEmpty()? priceOld : Double.valueOf(strPrice).doubleValue();

                //***********Begin************
                //调用Book类的setBook函数，把当前图书的信息重新设置为新信息
                booklist[id].setBook(title,author,press,ISBN,price);

                //************End*************);

                return true;
            }
            else {
                return false;
            }
        }

        void printAllBook(){ //循环打印所有的图书
            System.out.println("---------------------------------------------------------------------------");
            System.out.println("序号\t书名\t\t作者\t出版社\t\tISBN\t\t单价");
            System.out.println("---------------------------------------------------------------------------");
            for (int i = 0; i < count; i++){
                System.out.println((i+1)+"\t"+booklist[i].getTitle()+"\t"+booklist[i].getAuthor()+"\t"
                        +booklist[i].getPress() + "\t"+booklist[i].getISBN()+"\t"+booklist[i].getPrice());
            }
            System.out.println("---------------------------------------------------------------------------");
        }

        int findByOrder(int number){	//按序号(1~count)返回数组下标id

            if(number > 0 && number <= count ){

                return number - 1;
            }
            else
                return -1;
        }

        void findBook(){ //查询图书
            System.out.println("功能建设中...");
        }

        public static void main(String[] args) {

            Library myLibrary = new Library();
            myLibrary.run();
        }

    }



